package com.atguigu.service.impl;

import com.atguigu.mapper.SysMenuMapper;
import com.atguigu.mapper.SysUserMapper;
import com.atguigu.model.system.SysMenu;
import com.atguigu.model.system.SysUser;
import com.atguigu.service.SysMenuService;
import com.atguigu.service.SysUserService;
import com.atguigu.utils.MD5;
import com.atguigu.utils.MenuHelper;
import com.atguigu.vo.system.RouterVo;
import com.atguigu.vo.system.SysUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Override
    public IPage<SysUser> getPageList(IPage<SysUser> page, SysUserQueryVo sysUserQueryVo) {

        return sysUserMapper.getPageList(page,sysUserQueryVo);
    }

    @Override
    public void saveUser(SysUser sysUser) {
        sysUserMapper.saveUser(sysUser);
    }

    @Override
    public SysUser getUserById(Integer id) {
        return sysUserMapper.getUserById(id);
    }

    @Override
    public void updateUser(SysUser sysUser) {
        String password = sysUser.getPassword();
        if (!StringUtils.isEmpty(password)){
            password = MD5.encrypt(password);
            sysUser.setPassword(password);
        }
        sysUserMapper.updateUser(sysUser);
    }

    @Override
    public void updateStatus(SysUser sysUser) {
        sysUserMapper.updateStatus(sysUser);
    }

    @Override
    public Map<String, Object> getInfo(Long userId) {
        LambdaQueryWrapper<SysMenu> wrapper=new LambdaQueryWrapper<>();
        Map<String, Object> result=new HashMap<>();
        SysUser sysUser = sysUserMapper.getUserById(userId.intValue());
        List<SysMenu> sysMenus=null;
        if (userId.longValue()==1l){
            sysMenus=sysMenuMapper.selectList(wrapper.eq(SysMenu::getStatus,1).orderByAsc(SysMenu::getSortValue));
        }else{
         sysMenus = sysMenuMapper.getAllMenusByUserId(userId);

        }
        List<SysMenu> sysMenusTree = MenuHelper.buildTree(sysMenus);
        List<RouterVo> routerVoList=sysMenuService.getRouterVoList(sysMenusTree);
        List<String> permsList=sysMenuService.getPermsList(sysMenus,userId);
        result.put("name", sysUser.getName());
        result.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        //当前权限控制使用不到，我们暂时忽略
        result.put("roles",  new HashSet<>());
        result.put("buttons", permsList);
        result.put("routers", routerVoList);

        return result;

    }

    @Override
    public SysUser getByUsername(String assignee) {
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUser::getUsername,assignee);
        return baseMapper.selectOne(lambdaQueryWrapper);
    }


}
